package fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.HistoryEntity;

import java.util.List;

import dbdao.HistoryDBhelper;
import utils.adapter.CollectionAdpter;
import utils.adapter.HistoryDbAdpter;

/**
 * 历史 fragment
 * Created by dllo on 15/11/17.
 */
public class HistoryPageFragment extends AbsBaseFragment {
    private SwipeMenuListView listView;
    private HistoryDbAdpter adpter;
    private HistoryDBhelper dBhelper;
    private LayoutInflater inflater;
    private TextView clearhistory;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_history;
    }

    @Override
    protected void initView() {
        listView = findView(R.id.historyListView);
        dBhelper = HistoryDBhelper.getInstance();
        inflater = getActivity().getLayoutInflater();
        clearhistory = findView(R.id.clearhistory);
    }

    @Override
    protected void initData() {
        adpter = new HistoryDbAdpter();
        listView.setAdapter(adpter);
        adpter.addData(inflater, dBhelper.queryAll());
        //点击跳转的监听  进入播放页面
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //得到当前的
                goToPlay(dBhelper.queryAll().get(position).getTrackId());
            }
        });

        clearhistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "删除所有", Toast.LENGTH_SHORT).show();
                dBhelper.clearAll();

            }
        });
        //长按删除的方法
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                new AlertDialog.Builder(getContext())
                        .setTitle("删除")
                        .setMessage("确定要删除该条记录吗")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dBhelper.deleById(dBhelper.queryAll().get(position).getId());
                                HistoryDbAdpter adpter = new HistoryDbAdpter();
                                adpter.addData(inflater, dBhelper.queryAll());
                                listView.setAdapter(adpter);
                            }
                        }).create().show();

                return true;
            }
        });

        //滑动删除
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {

                SwipeMenuItem openItem = new SwipeMenuItem(
                        getContext());

                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(90);
                // set item title
                openItem.setTitle("查看");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth((90));
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        listView.setMenuCreator(creator);
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open
                        break;
                    case 1:
                        dBhelper.deleById(dBhelper.queryAll().get(position).getId());
                        HistoryDbAdpter adpter = new HistoryDbAdpter();
                        adpter.addData(inflater, dBhelper.queryAll());
                        listView.setAdapter(adpter);
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });

    }
}
